package cn.anc.aonicardv.module.ui.base;

import android.annotation.TargetApi;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkRequest;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.anc.aonicardv.Configs;
import cn.anc.aonicardv.Constant;
import cn.anc.aonicardv.util.ActivityUtils;
import cn.anc.aonicardv.util.LanguageUtil;
import cn.anc.aonicardv.util.LogUtil;
import cn.anc.aonicardv.util.SpUtils;
import cn.anc.aonicardv.util.StatusBarUtil;
import cn.anc.httpcontrolutil.SocketUtils;

import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET;
import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;

/**
 * Created by yangdai on 2017/5/16.
 */

public abstract class BaseActivity extends FragmentActivity {
    /**
     * 日志输出标志,当前类的类名
     **/
    protected final String TAG = this.getClass().getSimpleName();
    protected Unbinder unbinder;
    private Fragment lastFragment;
    protected Handler handler = new Handler();
    ConnectivityManager connectivityManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityUtils.addActivity(this);
        unbinder = ButterKnife.bind(this);
        StatusBarUtil.setTransparentForWindow(this);
        StatusBarUtil.setDarkMode(this);
        initVariable();
        setListener();
        initData();
    }

    public abstract void initVariable();

    public abstract void setListener();

    public abstract void initData();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != unbinder) {
            unbinder.unbind();
        }
        ActivityUtils.removeActivity(this);
    }

    public void showFragment(final Fragment to, int layout) {
        if (to == null)
            return;
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        Fragment f2 = getSupportFragmentManager().findFragmentByTag(to.getClass().getSimpleName());
        if (lastFragment != null) {
            fragmentTransaction.hide(lastFragment);
        }
        if (f2 == null) {
            fragmentTransaction.add(layout, to, to.getClass().getSimpleName());
            lastFragment = to;
        } else {
            fragmentTransaction.show(f2);
            lastFragment = f2;
        }
        fragmentTransaction.commit();
    }

    public Fragment getLastFragment() {
        return lastFragment;
    }





    @Override
    protected void attachBaseContext(Context newBase) {
        String selectedLanguage = SpUtils.getString(Constant.SpKeyParam.APP_LANUAGE, Configs.DEFUTL_LANGUAGE);
        LogUtil.e(TAG, "--------locale----1-------:" + selectedLanguage);
        super.attachBaseContext(LanguageUtil.attachBaseContext(newBase, selectedLanguage));
    }



    //处理失去焦点软键盘隐藏事件
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (!this.needDispatchTouchEventOnSoftKeyboard()) {
            return super.dispatchTouchEvent(ev);
        }
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        return getWindow().superDispatchTouchEvent(ev) || onTouchEvent(ev);
    }

    /**
     * 点击输入框外需要隐藏键盘           118452
     */
    private boolean isShouldHideInput(View v, MotionEvent ev) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = {0, 0};
            // 获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            // 点击的是输入框区域，保留点击EditText的事件
            return !(ev.getX() > left && ev.getX() < right && ev.getY() > top && ev.getY() < bottom);
        }
        return false;
    }

    /**
     * 是否需要处理软键盘失去焦点隐藏的事件 ,默认使用
     */
    protected boolean needDispatchTouchEventOnSoftKeyboard() {
        return true;
    }
}
